﻿using DocumentFormat.OpenXml.Lite.Core.Interfaces;
using DocumentFormat.OpenXml.Lite.Core.Office;
using DocumentFormat.OpenXml.Lite.WordprocessingML.Word.Base;
using DocumentFormat.OpenXml.Lite.Xml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using IO = System.IO;

namespace DocumentFormat.OpenXml.Lite.WordprocessingML.Word.Style
{
    [XmlRoot("styles", Namespace = Constants.WordMain)]
    public class Styles : XmlSerializable, IOfficeElement, ISerializable
    {
        [XmlIgnore]
        public Guid DocumentId { get; set; }

        [XmlElement("docDefaults", Namespace = Constants.WordMain)]
        public DefaultStyles DocumentDefaults { get; set; }

        [XmlElement("latentStyles", Namespace = Constants.WordMain)]
        public LatentStyles LatenStyles { get; set; }

        [XmlElement("", Namespace = Constants.WordMain)]
        public XmlList<StyleElement> ElementStyles { get; set; }

        [XmlNamespaceDeclarations]
        public XmlSerializerNamespaces Namespaces { get; set; }

        [XmlIgnore]
        public bool ElementStylesSpecified { get { return ElementStyles.Count > 0; } }

        public Styles()
        {
            ElementStyles = new XmlList<StyleElement>();

            Namespaces = new XmlSerializerNamespaces();
            Namespaces.Add("w", Constants.WordMain);
            //Namespaces.Add("xml", Constants.Xml);
            Namespaces.Add("r", Constants.Relationship);
        }

        public StyleElement CreateStyle()
        {
            StyleElement se = new StyleElement();
            ElementStyles.Add(se);
            return se;
        }

        #region ISerializable members

        public byte[] Serialize()
        {
            XmlSerializer serializer = new XmlSerializer(this.GetType());
            using (var stream = new IO.MemoryStream())
            {
                using (var writer = new IO.StreamWriter(stream, Encoding.UTF8))
                {
                    using (var xwriter = XmlWriter.Create(writer))
                    {
                        xwriter.WriteStartElement("w", "styles", Constants.WordMain);
                        foreach (var ns in Namespaces.ToArray())
                        {
                            if (ns.Name != "")
                            {
                                xwriter.WriteAttributeString("xmlns", ns.Name, null, ns.Namespace);
                            }
                        }
                        this.WriteXml(xwriter);
                    }
                    return stream.ToArray();
                }
            }
        }

        public void Deserialize(System.IO.Stream content)
        {
            using (var reader = XmlReader.Create(content))
            {
                reader.ReadToDescendant("styles", Constants.WordMain);
                for (int i = 0; i < reader.AttributeCount; ++i)
                {
                    string s = reader.GetAttribute(i);
                    if (Constants.GetDefaultPrefix(s) != string.Empty)
                    {
                        Namespaces.Add(Constants.GetDefaultPrefix(s), s);
                    }
                }
                this.ReadXml(reader);
            }
        }

        #endregion
    }
}
